/**
 * File: AbstractProcessor.java
 * Created by: mhaimel
 * Created on: 7 Jul 2009
 * CVS:  $Id: AbstractProcessor.java,v 1.2 2009/10/09 15:02:45 mhaimel Exp $
 */
package uk.ac.ebi.curtain.processor;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.ebi.curtain.CurtainContext;
import uk.ac.ebi.curtain.configuration.CurtainConfig;
import uk.ac.ebi.curtain.management.IProcessor;

import com.google.inject.Inject;

/**
 * @author mhaimel
 *
 */
public abstract class AbstractProcessor<I extends Serializable,O extends Serializable> implements IProcessor<I, O> {
	private Log log = null;
	private I input;
	private CurtainContext context;
	private CurtainConfig config;

	public void setInput(I input) {
		this.input = input;
	}
	
	protected I getInput() {
		return input;
	}
	
	public abstract void process();
	
	protected abstract O buildReturnObject();

	public O call() throws Exception {
		if(!isForceShutdown()){
			process();
		}
		if(!isForceShutdown()){
			return buildReturnObject();
		} else {
			return null;
		}
	}

	protected boolean isForceShutdown() {
		return Thread.currentThread().isInterrupted();
	}
	
	protected CurtainContext getContext() {
		return context;
	}
	
	@Inject
	public void setContext(CurtainContext context) {
		this.context = context;
	}
	
	protected CurtainConfig getConfig() {
		return config;
	}
	
	@Inject
	public void setConfig(CurtainConfig config) {
		this.config = config;
	}
	
	protected Log getLog() {
		if(null == log){
			log = LogFactory.getLog(this.getClass());
		}
		return log;
	}
}
